package com.dtsz.m2.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.CertificationVo;
import com.dtsz.m2.ui.model.TaskCount;
import com.dtsz.m2.ui.view.progress.WaterWaveProgress;
import com.dtsz.m2.utils.TaskCountdownUtils;
import com.dtsz.m2.volley.state.HUCState;

public class TaskAttestationActivity extends BaseActivity {
    private WaterWaveProgress waveProgress;
    private TextView tvTask, tvContent;
    private LinearLayout llTask;
    private TaskCount taskCount = new TaskCount();
    private TaskCountdownUtils taskCountdownUtils;

//    private ShowRewardAD showRewardAd;
    private final String TAG = "TaskAttestationActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_task_attestation);
        super.onCreate(savedInstanceState);
//        showRewardAd = new ShowRewardAD(this);
//        showRewardAd.setShowRewardADListener(getShowRewardListener());
        waveProgress = findViewById(R.id.waterWaveProgress1);
        waveProgress.setShowProgress(true);
        waveProgress.animateWave();
        waveProgress.setmShowText(true);
        tvActionbarTitle.setText("实名任务");
        getTaskCount();
    }

    @Override
    public void initView() {
        super.initView();
        tvTask = findViewById(R.id.tv_task);
        llTask = findViewById(R.id.ll_task);
        tvContent = findViewById(R.id.tv_content);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (showRewardAd != null) {
//            showRewardAd.destroy();
//        }
    }

    @Override
    public void initClick() {
        super.initClick();
        tvTask.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_task:
                String tvStr = tvTask.getText().toString();
                if ("开启实名认证".equals(tvStr)) {
                    initData2();
                }
                if ("开启实名权限".equals(tvStr)) {
                    tvTask.setEnabled(false);
                    loading();
//                    showRewardAd.loadAD("6000037");
                }
                break;
        }
    }


    private void getTaskCount() {
        //获取网络数据
        loading();
        pageVo.setType("实名任务");
        hucRequestInterfase.executeStr(this, TaskCount.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.TASKONEATTRES_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof TaskCount) {
                    TaskCount taskCount = (TaskCount) response;
                    int clickNum = Integer.parseInt(taskCount.getClickNum());
                    int num = Integer.parseInt(taskCount.getNum());
                    //waveProgress.setMaxProgress(clickNum);
                    int currentProgress = clickNum - num;
                    waveProgress.setProgress(num);
                    tvContent.setVisibility(View.VISIBLE);
                    tvContent.setText("提示：" + taskCount.getRemark());
                    if (currentProgress > 0) {
                        String isNum1 = taskCount.getClickNum1();
                        String clickFlag = taskCount.getClickFlag();
                        if ("0".equals(clickFlag)) {
                            if ("0".equals(isNum1)) {
                                tvTask.setText("开启实名权限");
                                tvTask.setEnabled(true);
                            } else {
                                tvTask.setEnabled(false);
                                taskCountdownUtils = new TaskCountdownUtils(this, tvTask, Long.parseLong(taskCount.getDateLong()), 1000);
                                taskCountdownUtils.setButtonClick("开启实名权限");
                                taskCountdownUtils.start();
                            }
                        } else {
                            tvTask.setText("开启实名权限");
                            tvTask.setEnabled(true);
                        }
                    } else {
                        tvTask.setText("开启实名认证");
                        tvTask.setEnabled(true);
                    }
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                String str = response.toString();
                if (str.indexOf("当前进度") != -1) {
                    getTaskCount();
                }
                showToast(str);
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                if (response instanceof CertificationVo) {
                    CertificationVo certificationVo = (CertificationVo) response;
                    if (certificationVo != null) {
                        String state = certificationVo.getBcState();
                        if ("-1".equals(state) || "不存在".equals(certificationVo.getBcDescription())) {
                            Intent intent1 = new Intent();
                            intent1.setClass(this, IdentificationCardActivity.class);
                            intent1.putExtra("name", certificationVo.getBcName());
                            intent1.putExtra("card", certificationVo.getBcCard());
                            //intent1.putExtra("phone", phone);
                            startActivity(intent1);
                            finish();
                        } else {
                            Intent intent1 = new Intent();
                            intent1.setClass(this, IdentificationCardTvActivity.class);
                            intent1.putExtra("name", certificationVo.getBcName());
                            intent1.putExtra("phone", certificationVo.getBcPhone());
                            intent1.putExtra("card", certificationVo.getBcCard());
                            intent1.putExtra("yhk", certificationVo.getBcYhk());
                            startActivity(intent1);
                        }
                    }
                }
                break;
        }
        loadDismiss();
    }

    public void getAddCount() {
        loading();
        taskCount.setToaType("实名任务");
        taskCount.setToken(pageVo.getToken());
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.ADDTASKATTRE_URL,
                hucParameInterfase.getRequestParameter(taskCount), true);
    }

    private void initData2() {
        loading();
        hucRequestInterfase.executeStr(this, CertificationVo.class,
                HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.ISCERTIFICATION_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

//    private ShowRewardADListener getShowRewardListener() {
//        return new ShowRewardADListener() {
//            @Override
//            public void onADLoad() {
//                Log.i(TAG, "onADLoad");
//            }
//
//            @Override
//            public void onADShow() {
//                Log.i(TAG, "onADShow");
//            }
//
//            @Override
//            public void onADExpose() {
//                Log.i(TAG, "onADExpose");
//            }
//
//            @Override
//            public void onReward() {
//                Log.i(TAG, "onReward");
//                getAddCount();
//                tvTask.setClickable(true);
//            }
//
//            @Override
//            public void onADClick() {
//                Log.i(TAG, "onADClick");
//            }
//
//            @Override
//            public void onNoAD(int errorCode) {
//                Log.i(TAG, "onError errorCode" + errorCode);
//                tvTask.setClickable(true);
//                loadDismiss();
//            }
//
//            @Override
//            public void onClose() {
//                Log.i(TAG, "onClose");
//            }
//        };
//    }

}
